
ac.lni('skill','金币挑战',{
    title = '|cffffff00金币挑战|r',
    show_cool = 1,
    cooldown_mode = 1,
    charge_max_stack = 3,
    art = [[ReplaceableTextures\CommandButtons\BTNMGExchange.blp]],
    tip = [[击败后获得大量|cffffff00金币|r|n当前怪物数量：|cff00ff00'get_maxcount'|r|n|n最多储存|cffd000cb3|r次,右键自动挑战]],
    charge_cool = 90,
    init_cool = 0,
    unit_name = '金币怪',
    unit_count = 5,
    max_count = 20,
})

ac.lni('skill','杀敌挑战',{
    title = '|cffff99cc杀敌挑战|r',
    art = [[ReplaceableTextures\CommandButtons\BTNRegeneration.blp]],
    tip = [[击败后获得大量|cffffff00杀敌|r|n当前怪物数量：|cff00ff00'get_maxcount'|r|n|n最多储存|cffd000cb3|r次,右键自动挑战]],
    show_cool = 1,
    cooldown_mode = 1,
    charge_max_stack = 3,
    unit_name = '杀敌怪',
    unit_count = 5,
    charge_cool = 180,
    init_cool = 0,
    max_count = 10,
})

ac.lni('skill','经验挑战',{
    title = '|cffcc99ff经验挑战|r',
    art = [[ReplaceableTextures\CommandButtons\BTNTomeBrown.blp]],
    tip = [[击败后获得大量|cffffff00经验|r|n当前怪物数量：|cff00ff00'get_maxcount'|r|n|n最多储存|cffd000cb3|r次,右键自动挑战]],
    show_cool = 1,
    cooldown_mode = 1,
    charge_max_stack = 3,
    unit_name = '经验怪',
    unit_count = 5,
    charge_cool = 120,
    init_cool = 0,
    max_count = 15,
})


ac.lni('skill','探索挑战',{
    title = '|cffccffcc探索挑战|r',
    art = [[ReplaceableTextures\CommandButtons\BTNGoblinSapper.blp]],
    tip = [[每只探索怪产出随机属性，概率掉落一件装备|n当前怪物数量：|cff00ff00'get_maxcount'|r|n|n最多储存|cffd000cb3|r次,右键自动挑战]],
    show_cool = 1,
    cooldown_mode = 1,
    charge_max_stack = 3,
    charge_cool = 150,
    unit_name = '探索怪',
    unit_count = 10,
    init_cool = 0,
    max_count = 12,

})

local ts_sx = {}
local ts_zb = {}
for key,v in pairs(load_excel('特殊奖池.xlsx','探索挑战')) do
    local data1 = v['参数1']
    local data2 = v['参数2']
    local data3 = v['参数3']
    local data4 = v['参数4']
    local data5 = v['参数5']
    if key:find('属性') then
        ts_sx[#ts_sx + 1] = {data1,data2,data3 or data3,data4,data5 or data4}
    else
        ts_zb[#ts_zb + 1] = {data1,data2,data3,data4 or 0}
    end
end

ac.game:event '英雄-探索奖励'(function(_,hero,level)
    local level = level or 0
    local player = hero.owner
    local data = table.random(ts_sx)
    local key = data[1]
    local min = data[2]
    local max = data[3] or min
    local ad1 = data[4] or 0
    local ad2 = data[5] or ad1
    local num = math.random(min + ad1 * level,max + ad2 * level)
    if hero:get_data('灵力萃取') then
        num = math.floor(num * 1.2)
    end
    hero:add(key,num)
    player:send_msg(('[迷宫]%s+%s'):format(key,num))
    if math.random(100)<=30 then
        local lv = math.floor(level/2)
        local weight = {}
        for _,da in ipairs(ts_zb) do
            if lv>=da[1] then
                weight[#weight + 1] = {da[2],da[3] + da[4]*lv}
            end
        end

        local lv = table.get_by_weight(weight)
        local name = random_item_name(lv)
        local item = collate_item(hero,name)
        player:send_msg(('[迷宫]额外获得物品:%s'):format(item:get_title()))

    end
end)


local list = {
    '金币挑战',
    '经验挑战',
    '杀敌挑战',
    --'探索挑战'
}

local levels = load_excel('数据投放.xlsx','挑战等级')
local params = load_excel('特殊奖池.xlsx','挑战资源')
for _,name in ipairs(list) do
    local mt = ac.skill[name]
    mt.physics = true
    mt.count = 0
    mt.exkey = ('%s怪数量'):format(name)

    function mt:on_add()
        self:set_stack(0)
        self:set_charge_cd(self.charge_cool)
        self:gc(ac.loop(1000,function()
            if self.show_effect and self:get_stack()>0 then
                self:start_cd_by_cast()
                self:on_cast_shot()
            end
        end))
    end
    
    function mt:get_maxcount()
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        return self.unit_count + hero:get(self.exkey)
    end

    function mt:on_cast_shot()
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        local name = self.unit_name
        self.count = self.count + 1
        local max = self:get_maxcount()
        local level = math.min(self.count,self.max_count)
        local lv = levels[level][name] or ac.enemy.level*6
        for a=1,max do
            local unit = player:create_enemy(name)
            local resource = ''
            if name=='金币怪' then
                resource = '金币'
            elseif name=='经验怪' then
                resource = '经验'
            elseif name=='杀敌怪' then
                resource = '杀敌'
            end
            if resource~='' then
                local info = params[resource]
                local min = info['最小值'] or 0
                local max = info['最大值'] or min
                local mina = info['最小提升'] or 0
                local maxa = info['最大提升'] or mina
                unit[resource] = math.random(min + level*mina,max + level*maxa)
            end
            unit:event '单位-死亡'(function()
                max = max - 1
                if name=='探索怪' then
                    --属性
                    hero:notify('英雄-探索奖励',hero,level)
                end
                if max==0 then
                    hero:notify('英雄-挑战完成',hero,self)
                end
            end)
        end
    end

end

ac.skill('委托挑战'){
    title = '虚空幻界',
    art = [[ReplaceableTextures\CommandButtons\BTNBundleOfLumber.blp]],
    tip = [[击败后获得大量|cff00ff00木材|r(最高|cffff000030层|r)
-|cffff0000F4|r取消挑战]],
    unit_name = '委托怪',
    count = 0,
    cool = 30,
    on_add = function(self)
        --self:set_cd(30)
        self:get_dis_art()
    end,
    title = function(self)
        return ('幻界|cffff0000%s|r层'):format(math.min(self.count+1,30))
    end,
    on_cast_shot = function(self)
        local unit = self.owner
        local player = unit.owner
        
        self:set_cd(0)
        if self.count==30 then
            return
        end
        local wait = 30
        local count = self.count + 1
        local level = levels[count]['委托'] or ac.enemy.level*6
        local file = levels[count]['委托模型']
        local aspd = levels[count]['委托范围']
        local mspd = levels[count]['委托移速']
        local model1 = get_unit_slk2(file,'file')
        local miss = get_unit_slk2(file,'Missileart')
        local size = get_unit_slk2(file,'modelScale')
        
        if self.wait_timer1 then
            self.wait_timer1:remove()
        end
        self.wait_timer1 = ac.wait(wait*1000,function(t)
            t:remove()
            self.wait_timer1 = nil
            self:set_enable(true)
            self.show_num = nil
            player:set_data('挑战层数',self.count)
            for _,unit in ipairs(self.unit_list) do
                unit:remove()
            end
        end)

        self:set_enable(false)
        self.show_num = '|cffff0000挑战中|r'
        self.unit_list = {}
        
        if count%5==0 then
            local enemy = player:create_enemy('委托精英')
            enemy:set_size(size)
            enemy:set_enemy_level(level)
            enemy:set_model(model1)
            enemy:add_effect([[Abilities\Spells\Items\AIil\AIilTarget.mdl]]):remove()
            enemy:set('攻击距离',aspd)
            enemy:set('移动速度',mspd)
            if miss and aspd>300 then
                enemy:add_buff '弹道设置'{model1 = miss}
            end
            self.unit_list[#self.unit_list + 1] = enemy
            enemy.title = ('委托第%d层'):format(count)
            enemy:add_buff '挑战限时'{time = wait}
            enemy:event '单位-死亡'(function()
                self.count = count
                player:add_icon('木材',150)
                if self.wait_timer1 then
                    self.wait_timer1:on_timer()
                end
            end)
        else
            local max = 3
            for a=1,max do
                local enemy = player:create_enemy('委托怪')
                enemy:set_size(size)
                enemy:set_enemy_level(level)
                enemy:set_model(model1)
                enemy:add_effect([[Abilities\Spells\Items\AIil\AIilTarget.mdl]]):remove()
                enemy:set('攻击距离',aspd)
                enemy:set('移动速度',mspd)
                if miss and aspd>300 then
                    enemy:add_buff '弹道设置'{model1 = miss}
                end
                self.unit_list[#self.unit_list + 1] = enemy
                enemy.title = ('委托第%d层'):format(count)
                enemy:add_buff '挑战限时'{time = wait}
                enemy:event '单位-死亡'(function()
                    max = max - 1
                    if max==0 then
                        self.count = count
                        player:add_icon('木材',150)
                        if self.wait_timer1 then
                            self.wait_timer1:on_timer()
                        end
                    end
                end)
            end
        end
    end
}


ac.game:event '游戏-关闭挑战'(function()
    ac.each_player(function(player)
        local list = player:get_data('挑战技能表') or {}
        for _,skill in ipairs(list) do
            skill.charge_max_stack = 0
            skill:set_stack(0)
            skill:set_cd(0)
            skill:set_art([[ReplaceableTextures\CommandButtons\BTNCancel.blp]])
            skill:set_enable(false)
            skill.disabled_art = skill:get_dis_art()
        end
    end)
end)

ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
    local book = {}
    for a,name in ipairs(list) do
        book[a] = hero:add_skill(name)
    end
    local skill = hero:add_skill('委托挑战')
    book[#book + 1] = skill
    
    player:set_data('委托挑战',skill)
    player:set_data('挑战技能表',book)
    player:notify('玩家-设置挑战内容',player,book)
end)


ac.sync:event '玩家-自动挑战'(function(_,player,index)
    local list = player:get_data('挑战技能表')
    local temp = list and list[index]
    if temp then
        temp.show_effect = not temp.show_effect
    end
end)

ac.sync:event '玩家-点击挑战'(function(_,player,index)
    local list = player:get_data('挑战技能表')
    local temp = list and list[index]
    if temp then
        temp:cast()
    end
end)


ac.sync:event '同步-放弃挑战'(function(_,player,flag)
    local skill = player:get_data('委托挑战')
    if skill and skill.wait_timer1 then
        skill.wait_timer1:on_timer()
    end
end)

ac.game:event '英雄-开始复活'(function(_,hero)
    ac.sync:notify('同步-放弃挑战',hero.owner,true)
end)

ac.hardware:event '键盘按下'(function(_,key)
    if key=='F4' then
        ac.sync('同步-放弃挑战')
    end
end)


